// import Node from './base';
import * as Util from '../js/util';

import Node, {Type, POS_H, POS_V, POS_P, POS_LABEL, TEXT_ALIGN, FONT_SIZE,
  FONT_LIST, ROTATE, AXIS_SYMBOL, LINE_TYPE, OPACITY, FONT_WEIGHT, POS,
  LAYOUT_HV, MAP_TYPE} from './base';

export function LabelExtra (parent) {
  return [
    new Node('标签旋转', 'rotate', parent, Type.Range, ROTATE),
    new Node('距离', 'distance', parent, Type.Value),
    new Node('偏移', 'offset', parent, Type.ArrayVal, [0, 0], ['横', '纵'])
  ];
}

// export function ItemStyleExtraBar (parent) {
//   return [
//     new Node('边框圆角', 'barBorderRadius', parent, Type.Value)
//   ];
// }

function Font (parent) {
  return [
    new Node('颜色', 'color', parent, Type.Color),
    new Node('字体', 'fontFamily', parent, Type.Select, FONT_LIST),
    new Node('字体大小', 'fontSize', parent, Type.Range, FONT_SIZE),
    new Node('字体粗细', 'fontWeight', parent, Type.Select, FONT_WEIGHT),
    new Node('描边颜色', 'textBorderColor', parent, Type.Color),
    new Node('线条宽度', 'textBorderWidth', parent, Type.Value)
    // new Node('水平对齐', 'align', parent, Type.Option, TEXT_ALIGN),
    // new Node('垂直对齐', 'verticalAlign', parent, Type.Option, POS_V),
    // 阴影及偏移 textShadowColor textShadowBlur textShadowOffsetX textShadowOffsetY
  ];
}

function Shadow (parent) {
  return [
    new Node('阴影颜色', 'shadowColor', parent, Type.Color),
    new Node('阴影大小', 'shadowBlur', parent, Type.Value),
    new Node('水平偏移', 'shadowOffsetX', parent, Type.Value),
    new Node('垂直偏移', 'shadowOffsetY', parent, Type.Value)
  ];
}

export function LineStyle (parent) {
  return [
    new Node('线条颜色', 'color', parent, Type.Color),
    new Node('线条类型', 'type', parent, Type.Option, LINE_TYPE),
    new Node('线条宽度', 'width', parent, Type.Value),
    new Node('线透明度', 'opacity', parent, Type.Range2, OPACITY),
    Shadow(parent)
  ];
}

function Margin (parent) {
  return [
    new Node('左间距', 'left', parent, Type.RangeValue),
    new Node('右间距', 'right', parent, Type.RangeValue),
    new Node('上间距', 'top', parent, Type.RangeValue),
    new Node('下间距', 'bottom', parent, Type.RangeValue)
  ];
}

// 以下为特定样式组装
export function Title (option) {
  return {
    'name': '标题',
    'children': Util.flat([
      new Node('标题', 'text', option.title, Type.String),
      new Node('副标题', 'subtext', option.title, Type.String),
      new Node('水平位置', 'left', option.title, Type.Option, POS_H),
      new Node('垂直位置', 'top', option.title, Type.Option, POS_V),
      Font(option.title.textStyle),
      new Node('副标颜色', 'color', option.title.subtextStyle, Type.Color),
      new Node('副标字体', 'fontFamily', option.title.subtextStyle, Type.Select, FONT_LIST),
      new Node('副标大小', 'fontSize', option.title.subtextStyle, Type.Range, FONT_SIZE)
    ])
  };
}

function Label (grandFather, name) {
  let parent = grandFather[name];
  return [
    new Node('显示标签', 'show', parent, Type.Boolean),
    new Node('位置', 'position', parent, Type.Select, POS_LABEL),
    new Node('格式化', 'formatter', parent, Type.String),
    new Node('字体', '', '', Type.Split),
    Font(parent),
    new Node('其它', '', '', Type.Split),
    new Node('背景颜色', 'backgroundColor', parent, Type.Color),
    new Node('边框颜色', 'borderColor', parent, Type.Color),
    new Node('边框宽度', 'borderWidth', parent, Type.Value),
    new Node('字块圆角', 'borderRadius', parent, Type.Value),
    // new Node('行高', 'lineHeight', parent, Type.String),
    Shadow(parent)
  ];
}

export function ItemStyle (grandFather, name) {
  let parent = grandFather[name];
  return [
    new Node('颜色', 'color', parent, Type.Color),
    new Node('边框颜色', 'borderColor', parent, Type.Color),
    new Node('边框宽度', 'borderWidth', parent, Type.Value),
    new Node('边框类型', 'borderType', parent, Type.Option, LINE_TYPE),
    Shadow(parent)
  ];
}

function Grid (option, name) {
  let parent = option[name];
  return [
    new Node('显示网格', 'show', parent, Type.Boolean),
    new Node('包含标签', 'containLabel', parent, Type.Boolean),
    Margin(parent),
    new Node('背景颜色', 'backgroundColor', parent, Type.Color),
    new Node('边框颜色', 'borderColor', parent, Type.Color),
    new Node('边框宽度', 'borderWidth', parent, Type.Value),
    Shadow(parent)
  ];
}

function AxisLabel (option, name) {
  let parent = option[name].axisLabel;
  return [
    new Node('显示标签', 'show', parent, Type.Boolean),
    new Node('标签间隔', 'interval', parent, Type.String),
    new Node('标签朝内', 'inside', parent, Type.Boolean),
    new Node('标签旋转', 'rotate', parent, Type.Range, ROTATE),
    new Node('距离', 'margin', parent, Type.Value),
    new Node('格式化', 'formatter', parent, Type.String),
    Font(parent)
  ];
}

function AxisTick (option, name) {
  let parent = option[name].axisTick;
  return [
    new Node('刻度', '', {}, Type.Split),
    new Node('刻度显示', 'show', parent, Type.Boolean),
    new Node('刻度间隔', 'interval', parent, Type.String),
    new Node('刻度朝内', 'inside', parent, Type.Boolean),
    new Node('刻度长度', 'length', parent, Type.Value),
    LineStyle(parent.lineStyle)
  ];
}

function AxisLine (option, name) {
  let parent = option[name].axisLine;
  return [
    new Node('显示轴', 'show', parent, Type.Boolean),
    new Node('轴箭头', 'symbol', parent, Type.ArraySel, AXIS_SYMBOL, ['首', '尾']),
    new Node('箭头大小', 'symbolSize', parent, Type.ArrayVal, [0, 100], ['宽', '高']),
    LineStyle(parent.lineStyle)
  ];
}

export function Position (parent) {
  return [
    // 待优化，增加option可选
    new Node('左间距', 'left', parent, Type.String),
    new Node('上间距', 'top', parent, Type.String),
    new Node('右间距', 'right', parent, Type.String),
    new Node('下间距', 'bottom', parent, Type.String)
  ];
}

function VisualMap (grandFather, name) {
  let parent = grandFather[name];
  return [
    new Node('组件类型', 'type', parent, Type.Option, MAP_TYPE),
    new Node('是否显示', 'show', parent, Type.Boolean),
    new Node('最小值', 'min', parent, Type.Value),
    new Node('最大值', 'max', parent, Type.Value),
    new Node('文本', 'text', parent, Type.ArrayText, ['', ''], ['首', '尾']),
    new Node('文本间距', 'textGap', parent, Type.Value),
    new Node('数据精度', 'precision', parent, Type.Value),
    new Node('是否反转', 'inverse', parent, Type.Boolean),
    new Node('实时更新', 'realtime', parent, Type.Boolean),
    new Node('显示手柄', 'calculable', parent, Type.Boolean),
    new Node('宽度', 'itemWidth', parent, Type.Value),
    new Node('高度', 'itemHeight', parent, Type.Value),
    new Node('手柄位置', 'align', parent, Type.Option, POS),
    new Node('维度', 'dimension', parent, Type.Value),
    new Node('放置', 'orient', parent, Type.Option, LAYOUT_HV),
    new Node('背景颜色', 'backgroundColor', parent, Type.Color),
    new Node('边框颜色', 'borderColor', parent, Type.Color),
    new Node('边框宽度', 'borderWidth', parent, Type.Value),
    new Node('映射颜色', 'color', parent.inRange, Type.Colors),
    new Node('字体', '', '', Type.Split),
    Font(parent)
  ];
}

// 属性集合
export function GetOption (name, parent) {
  let config = {
    'AxisLineX': ['X轴 - 轴线', 'xAxis', AxisLine],
    'AxisTickX': ['X轴 - 刻度', 'xAxis', AxisTick],
    'AxisLabelX': ['X轴 - 标签', 'xAxis', AxisLabel],
    'AxisLineY': ['Y轴 · 轴线', 'yAxis', AxisLine],
    'AxisTickY': ['Y轴 - 刻度', 'yAxis', AxisTick],
    'AxisLabelY': ['Y轴 - 标签', 'yAxis', AxisLabel],
    'Grid': ['图形网格', 'grid', Grid],
    'Label': ['图形标签', 'label', Label],
    'ItemStyle': ['条目样式', 'itemStyle', ItemStyle],
    'VisualMap': ['颜色映射', 'visualMap', VisualMap]
  };

  return {
    'name': config[name][0],
    'children': Util.flat(config[name][2](parent, config[name][1]))
  }
}

export function Common (option) {
  return [
    Title(option),
    GetOption('Grid', option),
    AxisY(option),
    AxisX(option),
    GetOption('AxisLineX', option),
    GetOption('AxisTickX', option),
    GetOption('AxisLabelX', option),
    GetOption('AxisLineY', option),
    GetOption('AxisTickY', option),
    GetOption('AxisLabelY', option)
  ]
}

export function GetStyle (option) {
  let style = Common(option);
  let label = GetOption('Label', option._series);
  let itemStyle = GetOption('ItemStyle', option._series);
  label.children = label.children.concat(LabelExtra(option._series.label));
  style.splice(1, 0, label);
  style.splice(1, 0, itemStyle);
  return style;
}

export function Axis (option, name) {
  let nodes = [
    new Node('显示', 'show', option[name], Type.Boolean),
    new Node('反向', 'inverse', option[name], Type.Boolean),
    new Node('文字', 'name', option[name], Type.String),
    new Node('文字距离', 'nameGap', option[name], Type.Value),
    new Node('文字旋转', 'nameRotate', option[name], Type.Range, ROTATE),
    new Node('文字位置', 'nameLocation', option[name], Type.Option, POS_P),
    new Node('上限', 'max', option[name], Type.Limit),
    new Node('下限', 'min', option[name], Type.Limit),

    // 分割线
    new Node('显示分割', 'show', option[name].splitLine, Type.Boolean),
    new Node('分割间隔', 'interval', option[name].splitLine, Type.String),
    // 分割线线条样式组
    LineStyle(option[name]['splitLine'].lineStyle)
  ];

  return {
    'name': '轴',
    'children': Util.flat(nodes)
  };
}

export function AxisX (option) {
  let axis = Axis(option, 'xAxis');
  return {
    'name': 'X轴 基础',
    'children': axis.children.concat([
      // new Node('Y轴', 'name', option.yAxis, Type.String),

    ])
  };
}

export function AxisY (option) {
  let axis = Axis(option, 'yAxis');
  return {
    'name': 'Y轴 基础',
    'children': axis.children.concat([
      new Node('Y轴', 'name', option.yAxis, Type.String)
    ])
  };
}

// 遗留问题：多series动态同步问题
export function SyncSeries (option) {
  option.series.forEach(function (s) {
    for (let key in option._series) {
      s[key] = option._series[key];
    }
    // s['label'] = option._series.label;
    // s['itemStyle'] = option._series.itemStyle;

  });
}
